<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/16
 * Time: 16:36
 */

namespace app\task\controller;


class Pushmsgtask
{

    public function task($key, $param)
    {
        if (empty($param)) {
            return 1;
        }
        $id = $param;
        $model = new \app\admin\model\PushMsg();
        $info = $model->getDataForWhere(array('id' => $id));
        if (empty($info)) {
            return 1;
        }
//        if ($info['is_avail_push'] == 0) {
//            return 2;
//        }
        if ($info['stats'] == 1) {
            return 3;
        }
//        print_r($info);
        //组装推送字符串start
        $title = $info['title'];
        $desc = $info['cont'];
        $val = $info['val'];//腾讯链接标识
        $ty = $info['fun'];

        $contTemp = array(
            'ty' => $ty,//1、单纯启动APP   2、进商品详情  3、 进活动页面  4、 进功能页面  5、金进入商家详情页面
            'title' => $title,
            'desc' => $desc,
            'val' => $val,
        );

        $ios_cont = array(
            'title' => $desc,
            'content' => $desc,
            "custom" => $contTemp,
            'accept_time' => array(),
            'aps' => array(
                'alert' => $desc,
                'badge' => 5,
                'category' => '',
                'custom1' => 'bar',
                "custom2" => $contTemp,
                "xg" => "oops"
            )
        );
        $cont = json_encode($contTemp, true);
        $msg = array(
            'title' => $title,
            'desc' => $desc,
            'cont' => $cont,
            'cont_ios' => $ios_cont,
            'custom_content' => $contTemp,
            'action' => array(
                'action_type' => 3,
                'activity' => '',
                'intent' => "xgscheme://com.xg.push/notify_detail_chunzhi?param1=" . $cont,//启动APP唯一标志，android
            ),
        );
        if ($info['ios']) {
            $userList = explode(",", $info['ios']);
            foreach ($userList as $k1 => $v1) {
                $res3 = pushTengXunSingle($msg, [$v1], 3);
            }

        }
        if ($info['ios_test']) {
            $userList = explode(",", $info['ios_test']);
            foreach ($userList as $k1 => $v1) {
                $res1 = pushTengXunSingle($msg, [$v1], 1);
            }
        }
        if ($info['android']) {
            $userList = explode(",", $info['android']);
            foreach ($userList as $k1 => $v1) {
                $res2 = pushTengXunSingle($msg, [$v1], 2);
            }
        }
        $resultString = $res3['err'] . "-" . $res2['err'] . "-" . $res1['err'];
        $currData = date("Y-m-d H:i:s");
        $model->updateDataForWhere(
            array('push_result' => $resultString, 'stats' => 1, 'ptime' => $currData),
            array('id' => $info['id']));
        return $resultString;
    }
}